/*
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codebar;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author yole
 */
public class YearTable extends javax.swing.JFrame {

    /**
     * Creates new form YearTable
     */
    private CodeList codeList;
    public YearTable(CodeList list) {
        initComponents();
        
        YearAction act = new YearAction();
        
        this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
        codeList = list;
        String [][] s = new String[codeList.size()][2];       

        String [] title = {"Ano","Ultimo processo"};
        
        for ( int i = 0; i < codeList.size(); i++ ) {
            s[i][0] = codeList.getYear(i);
            s[i][1] = "";
        }
        
        DefaultTableModel model = new DefaultTableModel(s,title);
        jtbYear.setModel(model);
        
        jbtOk.addActionListener(act);
        jbtCancel.addActionListener(act);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbtOk = new javax.swing.JButton();
        jbtCancel = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbYear = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jbtOk.setText("OK");

        jbtCancel.setText("Cancelar");

        jtbYear.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Ano", "Último Processo"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jtbYear);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jbtOk, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jbtCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtOk)
                    .addComponent(jbtCancel)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtCancel;
    private javax.swing.JButton jbtOk;
    private javax.swing.JTable jtbYear;
    // End of variables declaration//GEN-END:variables

   public class YearAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == jbtOk) {
                
                String s = new String("\n#######################\n"
                                      + "# PROCESSOS FALTANTES #\n"
                                      + "#######################\n");
                ArrayList<String> arrayproc;
                int n = jtbYear.getRowCount();
                int a = 0;
                int [][] proc = new int[2][n];
            
                for (int i = 0; i < n; i++) {
                    
                    proc[0][i] = Integer.parseInt((String.valueOf(jtbYear.getValueAt(i, 0))));
                    proc[1][i] = Integer.parseInt((String.valueOf(jtbYear.getValueAt(i, 1))));
                    arrayproc = verifyProcesses(proc[0][i],proc[1][i]);
                    
                    if (proc[1][i] > 0) {
                        s += "\nAno: " + proc[0][i];
                        s += "\n\nProcessos faltantes: ";
                        s += arrayproc.size() + "\n";
                        a += arrayproc.size();
                        for ( int j = 0; j < arrayproc.size(); j++ ) {
                            s += "\n" +arrayproc.get(j);
                        }
                    }
                    s += "\n";
                }
                
                s+="\n\n" + "TOTAL DE PROCESSOS FALTANTES: " + a;
                
                String aux = "#########################\n" +
                             "# PROCESSOS REGISTRADOS #\n" +
                             "#########################\n\n";
                
                Utils.export(aux + 
                        Utils.getLegibleContent(codeList)+s);
                
                dispose();
            }
            if (e.getSource() == jbtCancel) {
                dispose();
            }
        }
       
   }
   
   private ArrayList<String> verifyProcesses(int year, int lastProc) {
       ArrayList<String> proc = new ArrayList<String>();
       
       CodeNode N = codeList.get(String.valueOf(year));
       int first = 1;
       
       if (N != null) { 
       
       for ( int i = 0; i < N.size(); i++) {
           for ( int j = first; j < Integer.parseInt(N.getNOrdem(i)); j++)
               proc.add(Utils.forgeProc(j));
           
           first = Integer.parseInt(N.getNOrdem(i))+1;
       }
       
       for ( int i = first; i <= lastProc; i++) {
           proc.add(Utils.forgeProc(i));
       }
       
       return proc;
       
       }
       
       else return null;
       
   }

}
